//根据年月日计算星座
/*
function getAstro(month,day){
    var s="魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
    var arr=[20,19,21,21,21,22,23,23,23,23,22,22];
    return s.substr(month*2-(day<arr[month-1]?2:0),2);
}*/
$(document).ready(function () {
    var infoPath = "user/info/get";
    var params = {
        "SESSION_ID":SESSION_ID
    };
    var headImg = sessionStorage.getItem("user_head_img");
    if(headImg) {
        $("#user_head_img").attr("src",headImg);
    }
    request(infoPath,params,function (res) {
        var birthday = res.BODY.BIRTHDAY;
        $("#nickName").val(res.BODY.USER_NAME);
        $("#sex").val(res.BODY.GENDER == 1  ? "男" : "女");
        $("#birthday").val(birthday ? birthday.split(" ")[0] : "");
        linkage();
    });
})
function getAstro(m,d){
    return "魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯".substr(m*2-(d<"102223444433".charAt(m-1)- -19)*2,2);
}
$("#birthday").on("change",function () {
    linkage();
});
function linkage() {
    var date = $("#birthday").val();
    var arr = date.split("-");
    var m = "";
    var d = "";
    if(arr.length == 2) {
        m = arr[0];
        d = arr[1];
    } else {
        m = arr[1];
        d = arr[2];
        // $("#birthday").val(m + "-" + d);
    }

    $("#constellation").val(getAstro(m,d)+"座");
}
$(".nickname-blur").on("tap",function () {
    $("#nickName").blur();
});
$("#nickName").on("focus input",function () {
    var value = this.value;
    if(value) {
        $("#clearInput").css("display","inline-block");
    } else {
        $("#clearInput").css("display","none");
    }
});
$("#nickName").on("blur",function () {
    $("#clearInput").css("display","none");
});
$("#clearInput").on("tap",function () {
    $("#nickName").val("");
});
$("#save").on("tap",function () {
    var NICK_NAME = $("#nickName").val();
    var SEX = $("#sex").val() == "男" ? 1 : 2;
    var BIRTHDAY = $("#birthday").val();
    if(!NICK_NAME || !SEX || !BIRTHDAY) {
        return showErroMsg("请完善信息后再提交！",function(){
                $(".showErrorMassage").remove();
            });
    };
    var path = "user/info/update";
    var params = {
        "SESSION_ID":SESSION_ID,
        "NICK_NAME":NICK_NAME,
        "SEX":SEX,
        "BIRTHDAY":BIRTHDAY
    };
    request(path,params,function (res) {
        var flg = /^0+$/ig.test(res.RSPCD); //全零true 接口调用成功
        if(!flg) {
            return showErroMsg("保存失败！",function(){
                $(".showErrorMassage").remove();
            });
        } else {
            location.replace("./my.html");
        }
    })
});
$("#sex_edit").on("tap",function (e) {
    if(!($(e.target).attr("id") == "sex")) {
        $("#sex").trigger("click");
    }
})
$("#birthday_edit").on("tap",function (e) {
    if(!($(e.target).attr("id") == "birthday")) {
        $("#birthday").trigger("click");
    }
})
/*
$(".edit-text").on("tap",function (e) {
    // if(!($(e.target).attr("id") == "birthday")) {
        $("#birthday").trigger("click");
    // }
})*/
